<template>
  <div class="cityPage">
    <div class="selectCity">
      <span class="iconfont icon-arrowLeft" @click="gohome"></span>
      请选择城市
    </div>
    <div class="city">
      <div class="citys">
        <p>所在地址</p>
        <div class="path">
          <span @click="selectPath(path)">{{ path= $route.params.id? $route.params.id:$store.state.City.selectedCity}}</span>
        </div>
      </div>
      <div class="citys">
        <p>一级地点</p>
        <div v-for="(item,index) in CityData.cities" :key="index" @click="selectPath(item.name,index)">
          <span :class="[index===currentIndex?'bgcSpan':'']">{{item.name}}</span>
        </div>
      </div>
      <div class="citys">
        <p>二级级地点</p>
        <div v-for="(item,index) in CityData.hotCities" :key="index" @click="selectPath(item.name)">
          <span>{{item.name}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  components: {},
  props: {},
  data() {
    return {
      currentIndex:0,
      path:''
    };
  },
  computed: {
    ...mapState({
      CityData: state => state.City.data
    })
  },
  methods: {
    gohome() {
      this.$router.push({ path: "/" });
    },
    selectPath(e,i) {
      this.currentIndex = i
      this.$store.commit("changeCity", e);
      this.$router.push({path:'/'})
    }
  },
  beforeCreate() {
    document.body.style.backgroundColor = "#ffffff";
  },
  beforeDestroy: function() {
    document.body.style.backgroundColor = "#f5f5f5";
  },
  async mounted() {
      let backdata = await this.axios.get('http://localhost:8080/api/city.json')
      this.$store.commit('getCityData',backdata.data.data)
  }
};
</script>
<style lang="less" scoped>
.selectCity {
  text-align: center;
  position: relative;
  background-color: #f7f7f7;
  font-size: 20/37.5rem;
  padding: 10/37.5rem 0;
  span {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
  }
}
.iconfont {
  font-size: 20/37.5rem;
}
.city {
  padding: 10/37.5rem 10/37.5rem;
}
.citys {
  p {
    font-size: 16/37.5rem;
    margin-bottom: 10/37.5rem;
  }
  div {
    width: 33.333%;
    display: inline-block;
    padding: 0 15/37.5rem 15/37.5rem 0;
    box-sizing: border-box;
    span {
      display: inline-block;
      text-align: center;
      font-size: 16/37.5rem;
      padding: 10/37.5rem 0;
      width: 100%;
      border-radius: 20/37.5rem;
      background-color: #f4f4f4;
    }
  }
}
.path {
  position: relative;
  span {
    padding-left: 10px;
  }
  &::before {
    content: "";
    width: 16/37.5rem;
    height: 16/37.5rem;
    display: block;
    position: absolute;
    top: 25%;
    left: 10%;
    background-image: url("../assets/images/path.png");
    background-size: cover;
  }
}
.bgcSpan{
  background-color: #FFF7D1 !important;
  border:1px solid #FED724;
}
</style>